/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parismetro;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.animation.Timeline;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.scene.Group;
import javafx.scene.effect.MotionBlur;
import javafx.util.Math;
import modele.OrderList;
import javafx.date.DateTime;
import java.text.SimpleDateFormat;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import java.util.Locale;
import Logger.StartScreen;
import Logger.Logger;
import Logger.TaskScreen;
import javafx.animation.transition.SequentialTransition;
import javafx.animation.transition.PauseTransition;
import javafx.scene.paint.Color;

/**
 * @author Hadrien
 */
public var main: Main;
public def stationMachine = "Luxembourg";
public var logger = Logger {};
public var orderList: OrderList = OrderList {};

    public var startScreen:StartScreen = StartScreen {};
    public var taskScreen:TaskScreen = TaskScreen {};
    
var accueil: Accueil = Accueil {
            layoutX: 0;
            layoutY: 768;
        };
public var optionEcran: OptionEcran = OptionEcran {
            layoutX: 1024;
            layoutY: 0;
        };
var choixStation: ChoixStation = ChoixStation {
            layoutX: 1024;
            layoutY: 768;
        };
var ticketsSpeciaux: TicketsSpeciaux = TicketsSpeciaux {
            layoutX: 0;
            layoutY: 768 * 2;
        };
var optionSpeciale: OptionSpeciale = OptionSpeciale {
            layoutX: 1024;
            layoutY: 768 * 2;
        };
var paiement: Paiement = Paiement {
            layoutX: 1024 * 2;
            layoutY: 768;
        };
var aeroports: Aeroports = Aeroports {
            layoutX: 0;
            layoutY: 0;
        };
var merci: Merci = Merci {
            layoutX: 1024 * 2;
            layoutY: 768 * 2;
        };
public var isDialogDiplayed = false;
var isMoving = false;
public var dialog = DialogBox { hauteur: 400, largeur: 600 };

function run(__ARGS__: String[]) {

    var KeyEvent;

    main = Main {
                title: "Paris Metro"
                scene: Scene {
                    width: 1024
                    height: 768
                    content: []
                }
            }
}

public var menu: Group;
public var txtFooter: Text;
public var bordure = Color.WHITE;
public var animInfoTimer = SequentialTransition {content: [
        PauseTransition {duration: 5s},
        Timeline {
	repeatCount: Timeline.INDEFINITE
	keyFrames: [
		at (0s) { Main.bordure => Color.WHITE }
		at (1s) { Main.bordure => Color.BLACK }
		at (2s) { Main.bordure => Color.WHITE }
		at (4s) { Main.bordure => Color.WHITE }
	];
}
]}

public class Main extends Stage {

    var xTranslated: Integer = 0;
    var yTranslated: Integer = 768;
    var m_angle: Number = 0;
    var m_radius: Number = 0;
    var heure: String;
    var motion = MotionBlur {
                angle: bind m_angle;
                radius: bind m_radius;
            }
    public var tout: Group;


    function updateClock() {
        var dt = DateTime {}.instant;
        def hh = new SimpleDateFormat("EEEE d MMMM | H:mm:ss", Locale.FRANCE).format(dt);
        heure = hh;
    }


    init {
        menu = Group {
                    content: [accueil, optionEcran, choixStation, ticketsSpeciaux, optionSpeciale, paiement, aeroports, merci]
                    layoutX: bind -xTranslated;
                    layoutY: bind -yTranslated;
                    effect: motion;
                    disable: bind isDialogDiplayed or isMoving;
                };
        txtFooter = Text {
                    font: Font {
                        size: 24
                    }
                    x: 10, y: 760
                    content: bind "{stationMachine} | {heure}";
                }
        tout = Group {
                    content: [startScreen,taskScreen, menu, dialog, txtFooter]
                };
        taskScreen.visible = false;
        menu.visible = false;
        txtFooter.visible = false;
        this.scene.content = [tout];
        optionEcran.main = this;
        accueil.main = this;
        choixStation.main = this;
        ticketsSpeciaux.main = this;
        optionSpeciale.main = this;
        paiement.main = this;
        aeroports.main = this;
        merci.main = this;
        dialog.main = this;
        Timeline {
            repeatCount: Timeline.INDEFINITE
            keyFrames: KeyFrame {
                time: 1s
                action: function() {
                    updateClock();
                }
            }
        }.play();
    }

    public function changeContent(ecran: String): Void {
        Main.bordure = Color.WHITE;
        animInfoTimer.stop();
        animInfoTimer.playFromStart();
        if (ecran == "accueil") {
            moveTo(0, 768);
        } else if (ecran == "tplus") {
            moveTo(1024, 0);
        } else if (ecran == "rer") {
            moveTo(1024, 768);
        } else if (ecran == "speciaux") {
            moveTo(0, 768 * 2);
        } else if (ecran == "aeroports") {
            moveTo(0, 0);
        } else if (ecran == "optionSpeciaux") {
            moveTo(1024, 768 * 2);
        } else if (ecran == "paiement") {
            moveTo(2048, 768);
            paiement.timer();
        } else if (ecran == "merci") {
            moveTo(2048, 768 * 2);
            logger.currentTask.cart = orderList;
            logger.sendTask(logger.currentTask);
            insert logger.currentTask into logger.reachedTask;
            logger.currentTask = null;
            orderList.removeAll();
            changeContent("accueil");
            if(not logger.disable){
            menu.visible = false;
            txtFooter.visible = false;
            taskScreen.notifyTask();
            taskScreen.visible = true;
            }
            //merci.timer();
        }
    }

    function moveTo(x: Integer, y: Integer) {
        isMoving = true;
        m_radius = 40;
        if (xTranslated != x) {
            m_angle = Math.atan(((yTranslated - y) as Number) / ((xTranslated - x) as Number)) / Math.PI * 180;
        } else {
            m_angle = 90;
        }
        Timeline {
            repeatCount: 1
            keyFrames: [
                KeyFrame {
                    time: 700ms;
                    values: [xTranslated => x tween Interpolator.EASEBOTH, yTranslated => y tween Interpolator.EASEBOTH];
                    action: function() {
                        m_radius = 0;
                        m_angle = 0;
                        isMoving = false;
                    } }]
        }.play();

    }

}